package vip.specimen.system.service.question;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.datasource.entity.question.QuestionColumnEntity;
import vip.specimen.system.datasource.validation.question.ColumnValidation;
import vip.specimen.system.repository.question.ColumnRepository;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/4/22 17:36
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
@Transactional
public class ColumnService {
    @Resource
    private ColumnRepository columnRepository;
    @Resource
    private HttpServletRequest request;

    public QuestionColumnEntity findById(UUID uuid) {
        return this.columnRepository.findById(uuid).get();
    }

    /**
     * 新增答案分类
     *
     * @param columnValidation 验证体
     * @return 实体
     */
    public QuestionColumnEntity insert(ColumnValidation columnValidation) {
        QuestionColumnEntity superior = null;
        try {
            superior = null != columnValidation.getSuperior() ? this.columnRepository.findById(columnValidation.getSuperior()).get() : null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        QuestionColumnEntity daAnColumnEntity = new QuestionColumnEntity(
                columnValidation.getColumnName(), columnValidation.getSequence(), columnValidation.getColumnKeyword(),
                columnValidation.getColumnDescription(), columnValidation.isIndexIsShow(), superior
        );
        return this.columnRepository.save(daAnColumnEntity);
    }

    /**
     * 分页查询
     *
     * @return 实体列表
     */
    public Page<QuestionColumnEntity> findAllPage() {
        JSONObject param = CommonUtil.pageParams(this.request);
        return this.columnRepository.findAll(DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime")));

    }

    /**
     * 获取全部
     *
     * @return 实体列表
     */
    public List<QuestionColumnEntity> findAll() {
        return this.columnRepository.findAll(DataBaseUtil.getSort("desc", "createTime"));
    }

    /**
     * 根据状态
     *
     * @param uuid uuid
     * @return 实体
     */
    public QuestionColumnEntity update(UUID uuid) {
        if (this.columnRepository.existsById(uuid)) {
            QuestionColumnEntity columnEntity = this.columnRepository.findById(uuid).get();
            columnEntity.setEnable(!columnEntity.isEnable());
            columnEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.columnRepository.save(columnEntity);
        }
        return null;
    }

    /**
     * 更新排序
     *
     * @param uuid     uuid
     * @param sequence 序号
     * @return res
     */
    public QuestionColumnEntity update(UUID uuid, Integer sequence) {
        if (this.columnRepository.existsById(uuid)) {
            QuestionColumnEntity columnEntity = this.columnRepository.findById(uuid).get();
            columnEntity.setSequence(sequence);
            columnEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.columnRepository.save(columnEntity);
        }
        return null;
    }

    /**
     * 删除
     *
     * @param uuid uuid
     * @return res
     */
    public boolean delete(UUID uuid) {
        if (!this.columnRepository.existsById(uuid)) return false;
        this.columnRepository.deleteById(uuid);
        return true;
    }

    /**
     * 更新首页显示
     *
     * @param uuid uuid
     * @return 实体
     */
    public QuestionColumnEntity updateIndexIsShow(UUID uuid) {
        if (this.columnRepository.existsById(uuid)) {
            QuestionColumnEntity columnEntity = this.columnRepository.findById(uuid).get();
            columnEntity.setIndexIsShow(!columnEntity.isIndexIsShow());
            columnEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.columnRepository.save(columnEntity);
        }
        return null;
    }

    /**
     * 根据分类名称查询
     *
     * @param s 名称
     * @return 实体
     */
    public QuestionColumnEntity findByColumnName(String s) {
        return this.columnRepository.findByColumnName(s);
    }

    public int delete(Set<UUID> uuids) {
        int successNum = 0;
        for (UUID uuid : uuids) {
            successNum += this.delete(uuid) ? 1 : 0;
        }
        return successNum;
    }

    public QuestionColumnEntity update(UUID uuid, ColumnValidation columnValidation) {
        if (this.columnRepository.existsById(uuid)) {
            QuestionColumnEntity columnEntity = this.columnRepository.findById(uuid).get();
            columnEntity.setIndexIsShow(columnValidation.isIndexIsShow());
            columnEntity.setSequence(columnValidation.getSequence());
            columnEntity.setColumnDescription(columnValidation.getColumnDescription());
            columnEntity.setSequence(columnValidation.getSequence());
            QuestionColumnEntity superior = null;
            try {
                superior = null != columnValidation.getSuperior() ? this.columnRepository.findById(columnValidation.getSuperior()).get() : null;
            } catch (Exception e) {
                e.printStackTrace();
            }
            columnEntity.setSuperior(superior);
            columnEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.columnRepository.save(columnEntity);
        }
        return null;
    }
}
